/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.t4.irc.net.packet;

import com.t4.irc.IRC;
import com.t4.irc.Logger;
import com.t4.irc.net.packet.packets.*;

import java.util.HashMap;

/**
 * Handles packets
 * @author T4_
 */
public class PacketManager {

    /** Logger */
    private static Logger log = new Logger(PacketManager.class);

    /** The map of packets */
    private static HashMap<String, PacketImpl> packets = new HashMap<String, PacketImpl>();
    static {
        packets.put("PING", new Ping());
        packets.put("PRIVMSG", new Privmsg());
        packets.put("JOIN", new Join());
        packets.put("PART", new Part());
    }


    /**
     * Gets the packets source for a given message
     * @param msg
     * @return
     */
    private static String getPacketSource(String msg) {
        if(msg.startsWith(":"))
            return msg.substring(1).split(" ")[0];
        return "SERVER";
    }

    /**
     * Get the packets type for a given message
     * @param msg
     * @return
     */
    private static String getPacketType(String msg) {
        if(msg.startsWith(":")) {
            return msg.substring(1).split(" ")[1];
        } else {
            return msg.split(" ")[0].trim();
        }
    }

    /**
     * Gets the payload for a given message
     * @param msg
     * @return
     */
    private static String getPayload(String msg) {
        if(msg.startsWith(":")) {
            return msg.substring(1 + getPacketSource(msg).length() + 1 + getPacketType(msg).length()).trim();
        } else {
            return msg.substring(getPacketType(msg).length()).trim();
        }
    }

    /**
     * Gets the response for a given packets/msg
     * @param msg
     * @return
     */
    public static String getResponse(String msg) {
        try {
            String packetSource = getPacketSource(msg);
            String packetType = getPacketType(msg);
            String packetPayload = getPayload(msg);
            if(packets.containsKey(packetType)) {
                log.debug("Identified packets: " + packetType + ", payload: " + packetPayload + " from " + packetSource);
                return packets.get(packetType).getResponse(packetSource, packetPayload);
            }
            if(msg.contains(IRC.getConfiguration().getNickname() + " = " + IRC.getConfiguration().getChannel() + " :")) {
            	//Names packet
            	String names = packetPayload.split(":")[1];
            	IRC.getGui().pushText("> Users currently online: " + names);
            }
            log.debug("Unidentified packets: " + packetType + ", payload: " + packetPayload + " from " + packetSource);
            return null;
        } catch(Exception e) {
            log.error("Error parsing packets: " + msg);
            return null;
        }
    }
}
